import { nextTick } from 'vue';
import * as svg from '@element-plus/icons-vue';
import faConfig from '/@/assets/js/font-awesome.js';

const getElementPlusIconfont = () => {
	return new Promise((resolve, reject) => {
		nextTick(() => {
			const icons = svg as any;
			const sheetsIconList = [];
			for (const i in icons) {
				sheetsIconList.push(`ele-${icons[i].name}`);
			}
			if (sheetsIconList.length > 0) resolve(sheetsIconList);
			else reject('未获取到值，请刷新重试');
		});
	});
};

const getAwesomeIconfont = () => {
	return new Promise((resolve, reject) => {
		nextTick(() => {
			const icons = faConfig
			const sheetsIconList = [];
			for (const i in icons) {
				sheetsIconList.push(`${icons[i]}`);
			}
			if (sheetsIconList.length > 0) resolve(sheetsIconList);
			else reject('未获取到值，请刷新重试');
		});
	});
};

const getLocalIconfont = () => {
	return new Promise((resolve, reject) => {
		nextTick(() => {
			const sheetsIconList = [];
			const modulesFiles = import.meta.glob('../assets/icons/*.svg');
			Object.keys(modulesFiles).forEach((path) => {
				const key = path.replace(/^\..\/(.*)\.\w+$/, '$1').replace('assets/icons/', '');
				sheetsIconList.push(`local-icon-${key}`);
			});
			if (sheetsIconList.length > 0) resolve(sheetsIconList);
			else reject('未获取到值，请刷新重试');
		});
	});
};

/**
 * 获取字体图标 `document.styleSheets`
 * @method ele 获取 element plus 自带图标 `<i class="图标类名"></i>`
 * @method ali 获取 fontawesome 的图标 `<i class="图标类名"></i>`
 */
const initIconfont = {
	// element plus
	ele: () => {
		return getElementPlusIconfont();
	},
	// fontawesome
	awe: () => {
		return getAwesomeIconfont();
	},
	// local
	local: () => {
		return getLocalIconfont();
	},
};

// 导出方法
export default initIconfont;
